<?php
/**
 * Created by PhpStorm.
 * User: jucai002
 * Date: 2019/1/30
 * Time: 11:19
 */

namespace app\modules\admin\controllers;


use app\models\AdminJurisdiction;
use app\models\AdminUser;

class JurisdictionController extends DefaultController
{
    /**
     * @return string
     * 用户权限管理页面
     */
    public function actionIndex()
    {
        //判断用户是否为超级管理员
        if ($this->admin_user['grade'] == 0) {
            //查询出全部
            $admin_user = AdminUser::find()->where('id > 1')->asArray()->all();
        } elseif ($this->admin_user['grade'] == 1) {
            //查询出旗下的管理员
            $admin_user = AdminUser::find()->where(['superior_id' => $this->admin_user['id']])->asArray()->all();
        } else {
            return "没有权限";
        }
        foreach ($admin_user as $k => &$v) {
            if (empty($v['Jurisdiction_1'])) {
                $v['Jurisdiction_1'] = [['name' => '全部权限']];
            } else {
                $Jurisdiction_1 = explode(',', $v['Jurisdiction_1']);
                $jur = AdminJurisdiction::find()->where(['id' => $Jurisdiction_1])->asArray()->all();
                $v['Jurisdiction_1'] = $jur;
            }
        }
        return $this->render('index', [
            'head' => $this->Index(),
            'end' => $this->End(),
            'admin_user' => $admin_user,

        ]);
    }

    /**
     * @return int
     * 用户权限添加
     */
    public function actionAdd()
    {
        //查询出全部一级权限
        $jur_1 = AdminJurisdiction::find()->where(['level' => 0])->asArray()->all();
        $jur_2 = AdminJurisdiction::find()->where('level > 0')->asArray()->all();

        $tmp = [];
        foreach ($jur_2 as $k => $v) {
            $tmp[$v['level']]['list'][] = $v;
            foreach ($jur_1 as $key => $val) {
                if ($val['id'] == $v['level']) {
                    $tmp[$v['level']]['data'] = $val;
                }
            }
        }
        return $this->render('add', [
            'head' => $this->Index(),
            'end' => $this->End(),
            'grade' => $this->admin_user['grade'],
            'jur_1' => $jur_1,
            'jur_2' => $tmp,


        ]);
    }

    public function actionAdd_apply()
    {
        $username = \Yii::$app->request->post('username', false);
        $password = \Yii::$app->request->post('password', false);
        $repassword = \Yii::$app->request->post('repassword', false);
        $jur_1 = \Yii::$app->request->post('jur_1', '');
        $jur_2 = \Yii::$app->request->post('jur_2', '');
        if (empty($username) || empty($password) || empty($repassword)) {
            return $this->redirect(\Yii::$app->urlManager->createUrl(['admin/default/error', ['url' => '/admin/jurisdiction/add', 'message' => '参数不可为空']]));
        }
        if ($password != $repassword) {
            return $this->redirect(\Yii::$app->urlManager->createUrl(['admin/default/error', ['url' => '/admin/jurisdiction/add', 'message' => '确认密码必须和密码相同']]));
        }
        //查库 是否重复
        $userdata = AdminUser::find()->where(['username' => $username])->one();
        if (!empty($userdata)) {
            return $this->redirect(\Yii::$app->urlManager->createUrl(['admin/default/error', ['url' => '/admin/jurisdiction/add', 'message' => '该账户已存在，请重新添加新账户']]));
        }
        $admin_grade = $this->admin_user['grade'];
        if ($admin_grade != '0') {
            $grade = 2;
        } else {
            $grade = \Yii::$app->request->post('grade', 2);
        }
        $Jurisdiction_1 = '';
        $Jurisdiction_2 = '';
        if ($grade == 1) {
            $superior_id = 0;
        } else {
            $superior_id = $this->admin_user['id'];
            //1
            if (empty($jur_1)) {
                $jur_1 = ['1'];
            } else {
                if (!in_array('1', $jur_1)) {
                    $jur_1[] = '1';
                }
            };
            foreach ($jur_1 as $k => $v) {
                $Jurisdiction_1 .= $v.',';
            }
            $Jurisdiction_1 = trim($Jurisdiction_1,',');
            //2
            if(empty($jur_2)){
                $Jurisdiction_2 = '';
            }else{
                foreach ($jur_2 as $k => $v) {
                    $Jurisdiction_2 .= $v.',';
                }
                $Jurisdiction_2 = trim($Jurisdiction_2,',');
            }
        }

        $adminUser = new AdminUser();
        $adminUser->username = $username;
        $adminUser->password = $password;
        $adminUser->Jurisdiction_1 = $Jurisdiction_1;
        $adminUser->Jurisdiction_2 = $Jurisdiction_2;
        $adminUser->grade = $grade;
        $adminUser->superior_id = $superior_id;
        $adminUser->save(false);

        return $this->redirect(\Yii::$app->urlManager->createUrl(['admin/default/error', ['url' => '/admin/jurisdiction/index', 'message' => '添加成功']]));
    }


    public function actionSave()
    {
        $id = \Yii::$app->request->get('id');
        $admin_user = AdminUser::findOne($id);

        //查询出全部一级权限
        $jur_1 = AdminJurisdiction::find()->where(['level' => 0])->asArray()->all();
        $jur_2 = AdminJurisdiction::find()->where('level > 0')->asArray()->all();

        $tmp = [];
        foreach ($jur_2 as $k => $v) {
            $tmp[$v['level']]['list'][] = $v;
            foreach ($jur_1 as $key => $val) {
                if ($val['id'] == $v['level']) {
                    $tmp[$v['level']]['data'] = $val;
                }
            }
        }
        if(empty($admin_user->Jurisdiction_1)){
            $Jurisdiction_1 = false;
            $Jurisdiction_2 = false;
        }else{
            $Jurisdiction_1 = explode(',',$admin_user->Jurisdiction_1);
            $Jurisdiction_2 = explode(',',$admin_user->Jurisdiction_2);
        }
        return $this->render('save',[
            'head' => $this->Index(),
            'end' => $this->End(),
            'id'=> $id,
            'grade' => $this->admin_user['grade'],
            'jur_1' => $jur_1,
            'jur_2' => $tmp,
            'username'=>$admin_user->username,
            'Jurisdiction_1'=>$Jurisdiction_1,
            'Jurisdiction_2'=>$Jurisdiction_2,


        ]);
    }

    /**
     * 执行修改权限操作
     */
    public function actionSave_apply()
    {
        $id = \Yii::$app->request->post('id',false);
        if(empty($id)){
            return $this->redirect(\Yii::$app->urlManager->createUrl(['admin/default/error', ['url' => '/admin/jurisdiction/index', 'message' => 'ID不存在']]));
        }
        $jur_1 = \Yii::$app->request->post('jur_1', '');
        $jur_2 = \Yii::$app->request->post('jur_2', '');

        if (empty($jur_1)) {
            $jur_1 = ['1'];
        } else {
            if (!in_array('1', $jur_1)) {
                $jur_1[] = '1';
            }
        };
        $Jurisdiction_1 = '';
        foreach($jur_1 as $k=>$v){
            $Jurisdiction_1 .= $v.',';
        }
        $Jurisdiction_1 = trim($Jurisdiction_1,',');
        $Jurisdiction_2 = '';
        if(!empty($jur_2)){
            foreach($jur_2 as $k=>$v){
                $Jurisdiction_2 .= $v.',';
            }
            $Jurisdiction_2 = trim($Jurisdiction_2,',');
        }

        $admin_user = AdminUser::findOne($id);
        if(empty($admin_user)){
            return $this->redirect(\Yii::$app->urlManager->createUrl(['admin/default/error', ['url' => '/admin/jurisdiction/index', 'message' => 'ID不存在']]));
        }
        $admin_user->Jurisdiction_1 = $Jurisdiction_1;
        $admin_user->Jurisdiction_2 = $Jurisdiction_2;
        $admin_user->save(false);
        return $this->redirect(\Yii::$app->urlManager->createUrl(['admin/default/error', ['url' => '/admin/jurisdiction/index', 'message' => '修改权限成功']]));

    }



    /**
     * 所有权限列表
     */
    public function actionJ_list()
    {
        $jur = AdminJurisdiction::find()->where(['level' => 0])->asArray()->all();
        $array = AdminJurisdiction::find()->where('level > 0')->asArray()->all();

        $tmp = [];
        foreach ($array as $k => $v) {
            $tmp[$v['level']]['list'][] = $v;
            foreach ($jur as $key => $val) {
                if ($val['id'] == $v['level']) {
                    $tmp[$v['level']]['data'] = $val;
                }
            }
        }
        return $this->render('j_list', [
            'head' => $this->Index(),
            'end' => $this->End(),
            'data' => $jur,
            'array' => $tmp,

        ]);
    }

    /**
     * 添加新权限
     */
    public function actionJ_add()
    {
        $name = \Yii::$app->request->post('name', false);//权限名称
        $superior = \Yii::$app->request->post('superior', '0');//上级权限  0为顶级权限
        $route = \Yii::$app->request->post('route', false);///权限路由
        if (empty($name) || empty($route)) return $this->redirect(\Yii::$app->urlManager->createUrl(['admin/jurisdiction/j_list']));
        //执行权限添加
        $jur = new AdminJurisdiction();
        $jur->name = $name;
        $jur->level = $superior;
        $jur->route = $route;
        $jur->save(false);
        return $this->redirect(\Yii::$app->urlManager->createUrl(['admin/jurisdiction/j_list']));
    }

}